package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.resBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Reservation extends AppCompatActivity {

    private TextView TVVIP;
    private TextView TVNormal;
    private ListView listView;
    private ArrayList<Integer> arrayList = new ArrayList<>();
    private Gson gson;
    private resBean bean;
    String PName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reservation);
        TVVIP = findViewById(R.id.TV_VIP);
        TVNormal = findViewById(R.id.TV_Normal);
        listView = findViewById(R.id.recycleView);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/types/list?pageNum=1&pageSize=10&did=";
        loadMsg(1,newUrl);
        TVVIP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadMsg(1,newUrl);
            }
        });
        TVNormal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadMsg(2,newUrl);
            }
        });
        Intent intent = getIntent();
        PName = intent.getStringExtra("TV_Name");
    }

    public void loadMsg(int did,String newUrl){
        //清除旧数据
        arrayList.clear();
        OkHttpUtils.getCall(newUrl+did,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                gson = new Gson();
                ArrayList<resBean.RowsBean> tempList = new ArrayList<>();
                ArrayList<String> categoryNameList = new ArrayList<>();
                bean = gson.fromJson(response.body().string(),resBean.class);
                if (bean.getCode()==200){
                    List<resBean.RowsBean> list = bean.getRows();
                    if (list!=null){
                        tempList.addAll(list);
                    }
                    for (int i = 0; i < tempList.size(); i++) {
                        categoryNameList.add(tempList.get(i).getCategoryName());
                        arrayList.add(tempList.get(i).getId());
                    }
                    listView.post(new Runnable() {
                        @Override
                        public void run() {
                            listView.setAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,categoryNameList));
                        }
                    });
                }
            }
        });
        //设置点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(Reservation.this,ConformOrder.class);
                intent.putExtra("TV_userNName",PName);
                intent.putExtra("DepMart",bean.getRows().get(position).getCategoryName());
                intent.putExtra("money",bean.getRows().get(position).getMoney());
                intent.putExtra("id",bean.getRows().get(position).getDid());
                intent.putExtra("idH",bean.getRows().get(position).getId());
                startActivity(intent);
            }
        });
    }
}